package com.java46.carera.department;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java46.carera.base.entity.Department;
import com.java46.carera.base.entity.JsonResult;
import com.java46.carera.base.mapper.DepartmentMapper;
import com.java46.carera.base.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.List;

@Service
@Controller
public class DepartmentService {
    public DepartmentService(){
        System.out.println("DepartmentService...");
    }

    @Autowired
    private DepartmentMapper departmentMapper;

    // 添加
    public void addDepartment(String department_name, String department_introduction){
        Department department = new Department();   // 实例化 Department 部门类
        department.setDepname(department_name);
        department.setDepbrief(department_introduction);
        int addDepartment = departmentMapper.insert(department);
        if(addDepartment > 0) {
            JOptionPane.showMessageDialog(null, "添加成功");
        }else{
            JOptionPane.showMessageDialog(null, "添加失败!!!", "提示", JOptionPane.ERROR_MESSAGE);
        }
    }

    // 删除
    public void delDepartment(Integer department_id){
        int delDepartment = departmentMapper.deleteByPrimaryKey(department_id);
        if(delDepartment > 0){
            JOptionPane.showMessageDialog(null, "删除成功");
        }else{
            JOptionPane.showMessageDialog(null, "删除失败!!!", "提示", JOptionPane.ERROR_MESSAGE);
        }
    }


    // 修改前的查询
    public JsonResult selectDepartment(Integer department_id){
        Department department = departmentMapper.selectByPrimaryKey(department_id);
        return JsonUtil.setJsonResult(true, "",department);
    }

    // 修改
    public void modifyDepartment(Integer department_id, String department_name, String department_introduction){
        Department department = new Department();
        department.setDepname(department_name);
        department.setDepbrief(department_introduction);
        department.setDepid(department_id);

        int modifyDepartment = departmentMapper.updateByPrimaryKey(department);
        if(modifyDepartment > 0){
            JOptionPane.showMessageDialog(null, "修改成功");
        }
        else{
            JOptionPane.showMessageDialog(null, "修改失败！");
         }
    }


    // 查询所有
    public JsonResult selectDepartmentAll(Integer pagenNum, String depname){
        //获取第1页，5条内容，默认查询总数count
        PageHelper.startPage(pagenNum,4);
        List<Department> departments = departmentMapper.selectAll(depname);
        PageInfo<Department> page = new PageInfo<>(departments);
        // page.getTotal() 读取分页信息
        System.out.println(page.getSize());
        return JsonUtil.setJsonResult(true, "",page);
    }


    // 查询所有部门用于下拉框
    public JsonResult selectAllDep(){
        List<Department> departments = departmentMapper.selectAllDep();
        return JsonUtil.setJsonResult(true, "",departments);
    }

    // 用于员工
    public  Department selectDepartmentStaff(Integer department_id){
        Department department = departmentMapper.selectByPrimaryKey(department_id);
        return department;
    }
}
